﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CtrLayer;
using ModelLayer;

namespace GUILayer
{
    public partial class GUISale : Form
    {
        private SaleController saleCtr;
        private PersonController perCtr;
        ShowController showctr;
        private Sale currentSale;
        private Cinema currentCinema;
        private Room currentRoom;
        public GUISale(Cinema currentCinema, Room currentRoom)
        {
            InitializeComponent();
            saleCtr = new SaleController();
            perCtr = new PersonController();
            showctr = new ShowController();
            this.currentCinema = currentCinema;
            this.currentRoom = currentRoom;
            PopulateShowDropDown();
            textBoxSaleCinema.Text = currentCinema.Name;
            textBoxSaleRoom.Text = currentRoom.Name;
            textBoxSaleDiscount.Text = "0";
            textBoxSaleFreeSeats.Text = "" + CalculateAvailableSeats();
            radioButtonSaleCashSale.Select();
        }

        private void GUISale_Load(object sender, EventArgs e)
        {

        }

        private void buttonSaleBack_Click(object sender, EventArgs e)
        {
            Form newForm = new GUIStart();
            this.Hide();
            newForm.ShowDialog();
            this.Close();
        }

        private void labelSaleFreeSeats_Click(object sender, EventArgs e)
        {

        }

        private void buttonBookingUpdate_Click(object sender, EventArgs e)
        {
            int success = 0;
            if (currentSale != null && !currentSale.Payed)
            {
                try
                {
                    currentSale.SeatsReserved = int.Parse(textBoxSaleSeats.Text);
                }
                catch(Exception exp)
                {
                    MessageBox.Show("Ikke en gyldig mængde sæder");
                }
                success = saleCtr.UpdateSale(currentSale);
                if (success < 0)
                {
                    MessageBox.Show("Kan ikke opdatere nuværende reservation");
                }
            }
            else
            {
                MessageBox.Show("Kan ikke opdatere nuværende reservation");
            }

            ClearAll();
        }

        private void dateTimePickerBookingDate_ValueChanged(object sender, EventArgs e)
        {
            PopulateShowDropDown();
           
        }
        private void PopulateShowDropDown()
        {            
            DateTime d = dateTimePickerBookingDate.Value;
            DateTime start = new DateTime(d.Year, d.Month, d.Day, 0, 0, 0);
            DateTime end = new DateTime(d.Year, d.Month, (d.Day + 1), 0, 0, 0); 
            List<Show> ShowList = showctr.GetShows(start, end);
            int i = 0;
            while (i < ShowList.Count)
            {
                if (ShowList[i].DateTime.CompareTo(DateTime.Now) < 0)
                {
                    ShowList.RemoveAt(i);
                }
                else if (!(ShowList[i].Room.Equals(currentRoom) && ShowList[i].Room.Cinema.Equals(currentCinema)))
                {
                    ShowList.RemoveAt(i);
                }
                else
                {
                    i++;
                }
            }

            comboBoxSaleShow.Items.Clear();
            comboBoxSaleShow.Items.AddRange(ShowList.ToArray());
            InsertNextShow(ShowList);
            textBoxSaleFreeSeats.Text = "" + CalculateAvailableSeats();
        }

        private void buttonBookingBook_Click(object sender, EventArgs e)
        {
            if (radioButtonSaleCashSale.Checked)
            {
                MessageBox.Show("Der er valgt kontantsalg");
            }
            else
            {
                Person p = perCtr.GetPerson(textBoxSalePhoneNumber.Text);

                if (p == null)
                {
                    p = perCtr.CreatePerson(textBoxSaleFirstName.Text, textBoxSaleLastName.Text, textBoxSalePhoneNumber.Text);
                }

                currentSale = MakeSale(p);

                if (currentSale != null)
                {
                    saleCtr.InsertSale(currentSale);
                    textBoxSaleFreeSeats.Text = "" + CalculateAvailableSeats();
                }
                else
                {
                    MessageBox.Show("Kunne ikke oprette reservation");
                }

                ClearAll();
            }
        }

        private void buttonBookingSearch_Click(object sender, EventArgs e)
        {
            List<Sale> salesList = saleCtr.GetUnpaidSale(textBoxSalePhoneNumber.Text);

            if (salesList.Count == 0)
            {
                MessageBox.Show("Ingen reservation fundet");
            }
            else if (salesList.Count == 1)
            {
                currentSale = salesList[0];
                PopulateControlsFindSale();
            }
            else
            {
                // TODO: Skal testes
                Form bookingPopup = new GUISaleBookingPopup(salesList);
                bookingPopup.ShowDialog();
                currentSale = salesList[0];
                PopulateControlsFindSale();
            }
        }

        private void buttonBookingDelete_Click(object sender, EventArgs e)
        {
            if (currentSale != null)
            {
                int rc = saleCtr.DeleteSale(currentSale.SaleID);
                if (rc == 1)
                {
                    MessageBox.Show("Reservation slettet");
                    
                }
                else
                {
                    MessageBox.Show("Kunne ikke slette reservation");
                }               
            }
            ClearAll();
            textBoxSaleFreeSeats.Text = "" + CalculateAvailableSeats();
        }

        private void buttonSalePaid_Click(object sender, EventArgs e)
        {
            Person p = perCtr.GetPerson(1);

            if (currentSale != null && !currentSale.Payed)
            {
                currentSale.Payed = true;
                saleCtr.UpdateSale(currentSale);
            }
            else
            {
                currentSale = MakeSale(p);
                if (currentSale != null)
                {
                    currentSale.Payed = true;
                    saleCtr.InsertSale(currentSale);
                }
                else
                {
                    MessageBox.Show("Kunne ikke oprette salg");
                }
            }

            ClearAll();
            textBoxSaleFreeSeats.Text = "" + CalculateAvailableSeats();
        }

        private void comboBoxSaleShow_SelectedIndexChanged(object sender, EventArgs e)
        {    
            textBoxSaleFreeSeats.Text = "" + CalculateAvailableSeats();
            UpdatePrice();
        }

        private void InsertNextShow(List<Show> showList)
        {
            DateTime now = DateTime.Now;
            if (showList.Count > 0)
            {
                Show nextShow = showList[0];

                foreach (Show s in showList)
                {
                    if (nextShow.DateTime.CompareTo(s.DateTime) > 0)
                    {
                        nextShow = s;
                    }
                }
                if (nextShow != null)
                {
                    comboBoxSaleShow.SelectedItem = nextShow;
                }
                else
                {
                    // do nothing
                }
            }
            else
            {
                // do nothing
            }            
        }

        public int CalculateAvailableSeats()
        {
            int availableSeats = currentRoom.Seats;
            Show selectedSale;
            if (!(comboBoxSaleShow.SelectedIndex < 0))
            {
                selectedSale = (Show)comboBoxSaleShow.SelectedItem;

                List<Sale> salesList = saleCtr.GetSalesByShow(selectedSale);

                foreach (Sale s in salesList)
                {
                    availableSeats -= s.SeatsReserved;
                }
                return availableSeats;
            }
            else
            {
                return 0;
            } 

            //return 0;
        }

        private void PopulateControlsFindSale()
        {
            if (currentSale != null)
            {
                comboBoxSaleShow.Items.Clear();
                comboBoxSaleShow.Items.Add(currentSale.Show);
                comboBoxSaleShow.SelectedItem = currentSale.Show;

                textBoxSaleSeats.Text = "" + currentSale.SeatsReserved;
                textBoxSaleFirstName.Text = currentSale.Person.FName;
                textBoxSaleLastName.Text = currentSale.Person.LName;
                textBoxSalePhoneNumber.Text = currentSale.Person.Telephone;
                // Implementering af medlems stuff kan vente
                textBoxSalePrice.Text = "" + currentSale.TotalPrice;
            }
            else
            {
                // do nothing
            }
        }

        private void radioButtonSaleCashSale_CheckedChanged(object sender, EventArgs e)
        {
            textBoxSaleFirstName.ReadOnly = true;
            textBoxSaleLastName.ReadOnly = true;
            textBoxSalePhoneNumber.ReadOnly = true;

            textBoxSaleFirstName.Text = "Kontantsalg";
        }

        private void radioButtonSaleBooking_CheckedChanged(object sender, EventArgs e)
        {
            textBoxSaleFirstName.ReadOnly = false;
            textBoxSaleLastName.ReadOnly = false;
            textBoxSalePhoneNumber.ReadOnly = false;

            textBoxSaleFirstName.Text = "";
        }

        private Sale MakeSale(Person person)
        {
            int seatsToReserve = 0;
            bool makeSale = true;
            Sale s = null;
            if (comboBoxSaleShow.SelectedItem is Show)
            {
                Show show = (Show)comboBoxSaleShow.SelectedItem;
                decimal price = show.ShowPrice;
                //Person person;
                int discount = 0;
                //person = perCtr.GetPerson(textBoxSalePhoneNumber.Text);

                //if (person == null)
                //{
                //    person = perCtr.CreatePerson(textBoxSaleFirstName.Text, textBoxSaleLastName.Text, textBoxSalePhoneNumber.Text);
                //}

                try
                {
                    seatsToReserve = int.Parse(textBoxSaleSeats.Text);
                }
                catch (FormatException exp)
                {
                    makeSale = false;
                    MessageBox.Show("Ikke en gyldig mængde sæder");
                }

                try
                {
                    discount = int.Parse(textBoxSaleDiscount.Text);
                }
                catch (FormatException exp)
                {
                    makeSale = false;
                    MessageBox.Show("Ikke en gyldig rabat");
                }

                if (makeSale)
                {
                    price = (price * seatsToReserve) - discount;
                    s = saleCtr.CreateSale(price, show, person, false, DateTime.Now, seatsToReserve);

                    //saleCtr.InsertSale(currentSale);
                    //textBoxSaleFreeSeats.Text = "" + CalculateAvailableSeats();
                }
            }

            return s;
        }

        private void ClearAll()
        {
            currentSale = null;
            if (!radioButtonSaleCashSale.Checked)
            {
                textBoxSaleFirstName.Text = "";
            }
            textBoxSaleLastName.Text = "";
            textBoxSaleSeats.Text = "";
            textBoxSalePhoneNumber.Text = "";
            textBoxSaleDiscount.Text = "0";

        }

        private void textBoxSaleSeats_TextChanged(object sender, EventArgs e)
        {
            UpdatePrice();
        }

        private void textBoxSaleDiscount_TextChanged(object sender, EventArgs e)
        {
            UpdatePrice();
        }

        private void UpdatePrice()
        {
            int seats = 0;
            int discount = 0;
            try
            {
                seats = int.Parse(textBoxSaleSeats.Text);
                discount = int.Parse(textBoxSaleDiscount.Text);
                Show s = (Show)comboBoxSaleShow.SelectedItem;

                textBoxSalePrice.Text = "" + ((seats * s.ShowPrice) - discount);
            }
            catch (Exception exp)
            {
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ClearAll();
        }
    }
}
